/**
 * Created by Cicime on 2016/4/28.
 */

/* 页面滚动加载数据 */
var scroll = {
  page: 0,
  eventBind: function () {
    var byTop = $(document).scrollTop(),
        winH = $(window).height(),
        doH = $(document).outerHeight();
    if(doH - (byTop+winH) <= 40){
      this.ajaxData();
    }
  },
  ajaxData: function () {
    $.ajax({
      url: 'data.json',
      type: 'get',
      data: 'data'+ scroll.page,
      datatype: 'json',
      async: false,
      success: function(data){
        scroll.addNode(data);
        scroll.page++;
      },
      error: function(){
        alert('没联网~');
      }
    });
  },
  addNode: function (data) {
    var html = '',
        _html = '<section class="order">' +
                    '<h1>{{name}}</h1>' +
                    '<div class="order_line"></div>' +
                    '<label>' +
                      '<h2><span>{{dishName}}</span><em>{{number}}份</em><i>￥{{price}}</i></h2>' +
                      '<h3>{{time}}<i class="not-paying">{{status}}</i></h3>' +
                    '</label>' +
                    '<div class="order_line"></div>' +
                    '<ul>' +
                        '<li><a>删除订单</a></li>' +
                        '<li><a>立即支付</a></li>' +
                    '</ul>' +
                '</section>';

    for(var i=0; i<data.log.length; i++){
      html+= _html.replace(/{{name}}/g,data.log[i].name)
                  .replace(/{{dishName}}/g,data.log[i].dishName)
                  .replace(/{{number}}/g,data.log[i].number)
                  .replace(/{{price}}/g,data.log[i].price)
                  .replace(/{{time}}/g,data.log[i].time)
                  .replace(/{{status}}/g,data.log[i].status);
    }
    $('.content').append(html);
  },
  init: function () {
    scroll.eventBind();
  }
};


/* main */
$(function () {
  $(window).on('scroll',scroll.init);
});
